<template>
  <view class="container">
    <!-- 地址列表 -->
    <view class="address-list">
      <view class="address-item" v-for="address in addressList" :key="address.id" @click="selectAddress(address)">
        <view class="address-info">
          <view class="address-header">
            <text class="address-name">{{ address.name }}</text>
            <text class="address-phone">{{ address.phone }}</text>
          </view>
          <text class="address-detail">{{ address.address }}{{ address.detail }}</text>
        </view>
        <view class="address-actions">
          <text class="tag default" v-if="address.isDefault">默认</text>
          <text class="edit-icon" @click.stop="editAddress(address)">✏️</text>
          <text class="delete-icon" @click.stop="confirmDelete(address)">🗑️</text>
        </view>
      </view>
    </view>

    <!-- 底部按钮 -->
    <view class="footer">
      <button class="add-btn" @click="goToAdd">
        <text class="add-icon">+</text>
        <text>新增收货地址</text>
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { onShow } from '@dcloudio/uni-app'
import { getUserAddressList, deleteAddress as deleteUserAddress } from '@/api/address.js'

// 响应式状态
const addressList = ref([])
const isLoading = ref(false)

/**
 * 刷新地址列表数据
 */
const refreshAddressList = async () => {
  isLoading.value = true
  try {
    const res = await getUserAddressList()
    addressList.value = res.data || []
  } catch (error) {
    uni.showToast({
      title: '获取地址列表失败',
      icon: 'none'
    })
  } finally {
    isLoading.value = false
  }
}

/**
 * 选择地址并返回上一页
 * @param {Object} address - 选中的地址对象
 */
const selectAddress = (address) => {
  // 通过全局事件传递地址数据
  uni.$emit('addressSelected', address)
  uni.navigateBack()
}

/**
 * 编辑地址
 * @param {Object} address - 要编辑的地址对象
 */
const editAddress = (address) => {
  uni.navigateTo({
    url: `/pages/address/add?id=${address.id}`
  })
}

/**
 * 跳转到添加地址页面
 */
const confirmDelete = (address) => {
  uni.showModal({
    title: '确认删除',
    content: `确定要删除${address.name}的地址吗？`,
    success: async (res) => {
      if (res.confirm) {
        await deleteAddress(address)
      }
    }
  })
}

const deleteAddress = async (address) => {
  try {
    await deleteUserAddress(address.id)
    uni.showToast({
      title: '删除成功',
      icon: 'success'
    })
    refreshAddressList()
  } catch (error) {
    uni.showToast({
      title: '删除失败',
      icon: 'none'
    })
  }
}

const goToAdd = () => {
  uni.navigateTo({
    url: '/pages/address/add'
  })
}

// 页面显示时刷新数据
onShow(() => {
  refreshAddressList()
})
</script>

<style lang="scss" scoped>
.container {
  min-height: 100vh;
  padding-bottom: 120rpx;
  background: #f0f2f5;
}

/* 地址列表 */
.address-list {
  padding: 20rpx;
}

.address-item {
  margin-bottom: 20rpx;
  padding: 30rpx;
  background: #ffffff;
  border-radius: 20rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.address-info {
  margin-bottom: 20rpx;
}

.address-header {
  display: flex;
  align-items: center;
  gap: 20rpx;
  margin-bottom: 10rpx;
}

.address-name {
  font-size: 32rpx;
  font-weight: bold;
  color: #333;
}

.address-phone {
  font-size: 28rpx;
  color: #666;
}

.address-detail {
  font-size: 28rpx;
  color: #666;
  line-height: 1.4;
}

.address-actions {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.edit-icon {
  font-size: 32rpx;
  color: #666;
}

.tag {
  padding: 4rpx 12rpx;
  font-size: 24rpx;
  border-radius: 6rpx;
}

.tag.default {
  color: #ff4d4f;
  background: #fff1f0;
  border: 1rpx solid #ffccc7;
}

/* 底部按钮 */
.footer {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
  padding: 20rpx 30rpx;
  background: #ffffff;
  box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.08);
}

.add-btn {
  width: 100%;
  height: 80rpx;
  background: linear-gradient(135deg, #ff9a9e, #ff6b6b);
  border-radius: 40rpx;
  color: #fff;
  font-size: 28rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10rpx;
  box-shadow: 0 4rpx 12rpx rgba(255, 107, 107, 0.3);
}

.add-icon {
  font-size: 32rpx;
  font-weight: bold;
}
</style>